package com.guxingke.typecho4j.jfinal.config;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;

import com.guxingke.typecho4j.Typecho4j;
import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.controller.CoreController;
import com.guxingke.typecho4j.jfinal.handle.MyHandle;
import com.guxingke.typecho4j.jfinal.model.Comment;
import com.guxingke.typecho4j.jfinal.model.Content;
import com.guxingke.typecho4j.jfinal.model.Meta;
import com.guxingke.typecho4j.jfinal.model.Option;
import com.guxingke.typecho4j.jfinal.model.Relationships;
import com.guxingke.typecho4j.jfinal.model.User;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

/**
 * jfinal的配置类，也是typecho4j初始化的切入口<br/>
 * 
 * 类中方法均为模板方法，会被jfinal在合适的时间调用
 * 
 * @author guxingke
 */
public class Typecho4jConfig extends JFinalConfig {
	public void configConstant(Constants me) {
		loadPropertyFile("typecho4j.txt");
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.JSP);
	}

	public void configRoute(Routes me) {
		me.add("/", CoreController.class);
	}

	public void configPlugin(Plugins me) {
		C3p0Plugin c3p0Plugin = null;

		if (isOnMopaas()) {
			c3p0Plugin = new C3p0Plugin(
					getProperty("gitosc-jdbcUrl"),
					getProperty("gitosc-user"),
					getProperty("gitosc-password").trim());
		} else if (isOnCoding()) {
			c3p0Plugin = new C3p0Plugin(
					getProperty("coding-jdbcUrl"),
					getProperty("coding-user"),
					getProperty("coding-password").trim());
		} else {
			// 配置C3p0数据库连接池插件
			c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
					getProperty("user"), getProperty(
							"password").trim());
		}

		me.add(c3p0Plugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		// 是否打印sql语句
		arp.setDialect(new MysqlDialect()).setDevMode(true)
				.setShowSql(true);
		me.add(arp);

		arp.addMapping("typecho_contents", "cid", Content.class);
		arp.addMapping("typecho_metas", "mid", Meta.class);
		arp.addMapping("typecho_comments", "coid", Comment.class);
		arp.addMapping("typecho_options", "name", Option.class);
		arp.addMapping("typecho_users", "uid", User.class);
		arp.addMapping("typecho_relationships", Relationships.class);
	}

	/**
	 * 判断当前程序是否运行在mopaas演示平台上<br/>
	 * git@osc的默认演示平台
	 * 
	 * @return
	 */
	private boolean isOnMopaas() {
		Map<String, String> m = System.getenv();
		for (Entry<String, String> entry : m.entrySet()) {
			if ("VMC_APP_ID".equals(entry.getKey())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断是否在coding的演示平台上面
	 * 
	 * @return
	 */
	private boolean isOnCoding() {
		Map<String, String> m = System.getenv();
		for (Entry<String, String> entry : m.entrySet()) {
			if ("VCAP_APPLICATION".equals(entry.getKey())) {
				return true;
			}
		}
		return false;
	}

	public void configInterceptor(Interceptors me) {

	}

	public void configHandler(Handlers me) {
		me.add(new MyHandle());
	}

	/**
	 * 在jfinal初始启动后，初始化typecho4j
	 */
	@Override
	public void afterJFinalStart() {
		JFinal jfinal = JFinal.me();
		ServletContext applicatContext = jfinal.getServletContext();
		Typecho4j typecho4j = Typecho4j.typecho4j();
		try {
			typecho4j.init(applicatContext);
		} catch (Typecho4jException e) {
			System.out.println("typecho4j初始化失败,系统自动关闭  "
					+ e.getMessage());
			System.exit(0);
		}
	}
}
